<?php
   // include function files for this application
   require_once('/home/domains/uny-con.com/public_html/uny-con_fns.php');
   // start session which may be needed later
   // start it now because it must go before headers
   if (!session_id()) { session_start(); }


  if (!check_valid_user()) {
     // they are not logged in 
     do_html_header('You must be logged in', staysmall, $uny);
     echo 'You are not logged in.<br />';
     do_html_url('login.php', 'Login');
	 print_brs(6);
     do_html_footer(news);
     exit;
   }

// Check if event registration is open
  if ($conf['event_sub_status'] == 'closed' && $_SESSION['admin'] != 't') {
     do_html_header('Event Submission Closed', staysmall, $uny);
     echo 'Event Submission is currently closed.<br />';
	 print_brs(6);
     do_html_footer(news);
     exit;
   }

  //If the post vars are not set and no event iD is in Get_Vars then they must be trying to submit a new event with us so display the form with no modification.
  if (empty($_POST) && empty($_GET)) {
  do_html_header('Event submission page', gobig, $uny);

  display_event_form();

  do_html_footer(no_news);
  exit;
  }

  //If _POST is empty and _GET['event_id'] is set then we need to get the event data and display the form for editing. It occurs to me once pre-reg starts we need to be extra careful about changing the timeslot of something lest we change many users schedules who have registered for an event...
  if (isset($_GET['event_id']) && empty($_POST) && check_valid_user()) {
     $_SESSION['event_id'] = $_GET['event_id'];
     $event_values = recordproc(get_event_details($_SESSION['event_id']));
     $event_values['update'] = 'Y';
     do_html_header('Update your event', gobig, $uny);
	     if ($event_values['attendee_id'] == $_SESSION['current_attendee_id'] || $_SESSION['admin'] == 't') {
       		display_event_form($event_values);
     		}
     else {
        echo 'You are trying to edit an event that does not belong to you.<br />';
     	}
     do_html_footer(no_news);
     exit;
     }


  //create short variable name
	$event_values['event_title']=trim($_POST['event_title']);
	$event_values['event_type']=trim($_POST['event_type']);
	$event_values['game_system']=trim($_POST['game_system']);
	$event_values['min_players']=floatval($_POST['min_players']);
	$event_values['max_players']=floatval($_POST['max_players']);
	$event_values['difficulty']=floatval($_POST['difficulty']);
	$event_values['familiarity']=floatval($_POST['familiarity']);
	$event_values['maturity']=trim($_POST['maturity']);
	$event_values['num_of_tables']=floatval($_POST['num_of_tables']);
	$event_values['cost']=str_replace ( ",", "", $_POST['cost']);
	$event_values['cost']=floatval($event_values['cost']);
	$event_values['equipment']=trim($_POST['equipment']);
	$event_values['notes']=trim($_POST['notes']);
	$event_values['timestamp']=trim($_POST['timestamp']);
	$event_values['duration']=$_POST['duration'];
	$event_values['update']=$_POST['update'];
	$event_values['approved']=trim($_POST['approved']);
	$event_values['display_org']=trim($_POST['display_org']);
	$event_values['GM']=trim($_POST['GM']);
	$event_values['show_change']=trim($_POST['show_change']);
	$event_values['featured']=trim($_POST['featured']);
	$event_values['cancelled']=trim($_POST['cancelled']);
	$event_values['change_description']=trim($_POST['change_description']);
	$event_values['assigned_room']=trim($_POST['assigned_room']);
	$event_values['description']=trim($_POST['description']);
	$event_values['multi_session']=$_POST['multi_session'];
	$event_values['multi_start_time']=$_POST['multi_start_time'];
	$event_values['description']=str_replace("  ", " ", $event_values['description']);
	$event_values['description']=str_replace("  ", " ", $event_values['description']);
	$event_values['description']=str_replace("  ", " ", $event_values['description']);
	$event_values['description']=str_replace("\r\n", "\n", $event_values['description']);
	$event_values['description']=str_replace("\r", "\n", $event_values['description']);
	$event_values['description']=str_replace("\n\n", "\n", $event_values['description']);
	$event_values['description']=str_replace("\n\n", "\n", $event_values['description']);
	$event_values['description']=str_replace("\n ", "\n", $event_values['description']);
	if(!is_array($event_values['multi_start_time'])) { $event_values['multi_start_time'] = array($event_values['multi_start_time']); }
	
  //Set values for untoggled fields
  	if((!isset($event_values['display_org'])) || ($event_values['display_org'] == "")) { $event_values['display_org'] = "f";}
	
  //Set which fields are required
	$required['event_title']=$event_values['event_title'];
	$required['event_type']=$event_values['event_type'];
	$required['game_system']=$event_values['game_system'];
	$required['min_players']=$event_values['min_players'];
	$required['max_players']=$event_values['max_players'];
	$required['difficulty']=$event_values['difficulty'];
	$required['familiarity']=$event_values['familiarity'];
	$required['maturity']=$event_values['maturity'];
	$required['description']=$event_values['description'];
	$required['multi_start_time']=$event_values['multi_start_time'];
	$required['duration']=$event_values['duration'];
	
  //Set error messages for each required value
	$error_msg['event_title']="<li>You must specify an event title.</li>";
	$error_msg['event_type']="<li>You must specify an event type.</li>";
	$error_msg['game_system']="<li>You must specify a game system.</li>";
	$error_msg['min_players']="<li>You must specify the minimum number of players.</li>";
	$error_msg['max_players']="<li>You must specify the maximum number of players.</li>";
	$error_msg['difficulty']="<li>You must specify the difficult level.</li>";
	$error_msg['familiarity']="<li>You must specify the required familiarity with the system.</li>";
	$error_msg['maturity']="<li>You must specify a maturity level.</li>";
	$error_msg['multi_start_time']="<li>You must provide a starting time for your event.</li>";
	$error_msg['duration']="<li>You must specify a duration for your event</li>";
	$error_msg['description']="<li>You must provide a description for your event.</li>";

 do_html_header('Submitting event', gobig, $uny);


  if (!filled_out($required)) {
    echo '<span class="redtext">The form could not be submitted because the following errors were discovered with your submission:</span><br />';
    echo "<ul class=\"redtext\">";
    print_error_list($required, $error_msg);
    echo "</ul>";
    display_event_form($event_values);
    do_html_footer(no_news);  
    exit;
  }

  if ($event_values['max_players'] < $event_values['min_players']) {
    echo 'The maximum number of players must be greater than the minimum number of players.<br />';
    display_event_form($event_values, gobig, $uny);
    do_html_footer();  
    exit;
  }

  if ($event_values['update'] == 'Y') {
  // try to update event
    if (update_event($_SESSION['event_id'], $event_values)) {
      echo 'Event '.$event_values['event_id'].' was updated.<br>';
      if ($event_values['approved'] == 't' && $_SESSION['admin'] == 't') {
          mail_event_confirmation(approved, $_SESSION['event_id']);
          echo 'A confirmation email has been sent.<br>'; 
      print_event_details(get_event(eventid, $_SESSION['event_id']));
      unset($_SESSION['event_id']);
      }
    }
    else {
      echo 'Could not Update Event.<br> <br><br><br><br> <br><br><br><br><br> <br><br><br><br> <br><br><br><br><br>';
    }
  }
  else 
  {
      if(($event_values['multi_session'] !== "1") && (!isset($_POST['multi'])))
  		{
  			echo 'Select the start times for each of your sessions';
  			//echo "\n multi_session = $event_values['multi_session']\n ";
  			//echo "\n multi_start_time = $event_values['multi_start_time']\n ";
  			echo "<form method=post action=\"submit_event.php\">\n<table>";
  			for($i=1; $i<=$event_values['multi_session']; $i++)
  			{
  				echo '<tr><td';
  				if (!isset($grey)) { echo ' class = "grey_back"'; $grey = "grey"; }
  				else { unset($grey); }
  				echo ">";
  				$j = $i-1;
  				echo "<select name=\"multi_start_time[]\">";
  				$time = $_POST['multi_start_time'];
  				$time = $time["$j"];
  				echo "time = $time";
  				print_starting_time_list($time);
  				echo "\t\t</select>";
  				echo "</td></tr>\n";
  			}
  			echo "</table>\n";
  			if ($_SESSION['admin'] != 't') 
  			{
  				echo "<input type='hidden' name='approved' value=\"";
  				if ($event_values['approved'] == 't') { echo 't'; } 
  				else {echo 'f';} 
  				echo "\">\n";
				echo "<input type='hidden' name='featured' value=\"";
				if ($event_values['featured'] == 't') { echo 't'; }
				else { echo 'f'; }
				echo "\">\n";
				echo "<input type='hidden' name='cancelled' value=\"";
				if ($event_values['cancelled'] == 't') { echo 't'; } 
				else { echo 'f'; }
				echo "\">\n";
				echo "<input type='hidden' name='show_change' value=\"";
				if ($event_values['cshow_change'] == 't') { echo 't'; } 
				else { echo 'f'; }
				echo "\">\n";
				echo "<input type='hidden' name='change_description' value=\"";
				echo htmlspecialchars(stripslashes($event_values['change_description']));
				echo "\">\n";
			}
			echo "<input type='hidden' name='timestamp' value='now()'>\n";
			echo "<input type='hidden' name='update' value='";
			if ($event_values['update'] == 'Y') { echo 'Y'; } 
			else { echo 'N'; }
			echo "'>\n";
			foreach($_POST as $post => $info)
			{
				if(!is_array($info)) {
					$info = htmlspecialchars(stripslashes($info), ENT_QUOTES);
					echo "\t\t<input type='hidden' name='$post' value='$info'>\n"; 
					}
			}
			echo "<input type='hidden' name='multi' value='t'>\n";
			echo '<input type=submit value="Submit Event">';
  			echo "</form>\n";
  		}
      else 
      {
     // try to add event, first check and make sure that all the start time values are unique.
     $mstc = count($_POST['multi_start_time']);
     $unique_mstc = count(array_unique($_POST['multi_start_time']));
     if ($mstc == $unique_mstc) 
     {
      if ($event_id = add_event($event_values)) 
      {
        echo 'Your event has been submitted successfully.<br>'; 
        if (mail_event_confirmation(initialsubmit, $event_id)) {
          echo 'A confirmation email has been sent.<br>'; 
          }
        else {
          echo 'Could not send email.';
          print_brs(13);
          }
      print_event_details(get_event(eventid, $event_id));
      echo "<br />\n <a href=\"submit_event.php\">Submit another event?</a>\n";
      }
      else {
        echo 'Could not submit event.<br>';
            display_event_form($event_values);
            }
     }
     else {
		echo '<span class="redtext">Cannot submit duplicate starting times!</span>';
		display_event_form($event_values);
     }
        }
      }
  do_html_footer(no_news);
  ?>
  